clear
insheet using "$sourcedatadir/wtid/topincomes.csv"
encode country, gen(ctry)
sort ctry year
tsset ctry year

// Interpolate the German data as there are three-year gaps.
// Only do it from 1961 as the gap before that is very long
ipolate top5averageincome year if country == "Germany" & year > 1960, gen(temp)
replace top5averageincome = temp if temp != .
drop temp
ipolate top1averageincome year if country == "Germany" & year > 1960, gen(temp)
replace top1averageincome = temp if temp != .
drop temp
ipolate top10averageincome year if country == "Germany" & year > 1960, gen(temp)
replace top10averageincome = temp if temp != .
drop temp
ipolate top01averageincome year if country == "Germany" & year > 1960, gen(temp)
replace top01averageincome = temp if temp != .
drop temp
ipolate top05averageincome year if country == "Germany" & year > 1960, gen(temp)
replace top05averageincome = temp if temp != .
drop temp
ipolate top01averageincome year if country == "Germany" & year > 1960, gen(temp)
replace top01averageincome = temp if temp != .
drop temp
ipolate top001averageincome year if country == "Germany" & year > 1960, gen(temp)
replace top001averageincome = temp if temp != .
drop temp
ipolate bottom90averageincome year if country == "Germany" & year > 1960, gen(temp)
replace bottom90averageincome = temp if temp != .
drop temp

// Interpolate the Dutch data as there are two- and three-year gaps.
// Only do it from 1950 as the gap before that is very long
ipolate top10averageincome year if country == "Netherlands" & year > 1949, gen(temp)
replace top10averageincome = temp if temp != .
drop temp
ipolate top5averageincome year if country == "Netherlands" & year > 1949, gen(temp)
replace top5averageincome = temp if temp != .
drop temp
ipolate top1averageincome year if country == "Netherlands" & year > 1949, gen(temp)
replace top1averageincome = temp if temp != .
drop temp
ipolate top05averageincome year if country == "Netherlands" & year > 1949, gen(temp)
replace top05averageincome = temp if temp != .
drop temp
ipolate top01averageincome year if country == "Netherlands" & year > 1949, gen(temp)
replace top01averageincome = temp if temp != .
drop temp
*ipolate top001averageincome year if country == "Netherlands" & year > 1949, gen(temp)
*replace top001averageincome = temp if temp != .
*drop temp
ipolate bottom90averageincome year if country == "Netherlands" & year > 1949, gen(temp)
replace bottom90averageincome = temp if temp != .
drop temp
ipolate averageincomepertaxunit year if country == "Netherlands" & year > 1949, gen(temp)
replace averageincomepertaxunit = temp if temp != .
drop temp


gen     wtid_incgrowthtop10 = (top10averageincome - l.top10averageincome)/l.top10averageincome
replace wtid_incgrowthtop10 = (top10averageincomeadults - l.top10averageincomeadults)/l.top10averageincomeadults if wtid_incgrowthtop10 == .
replace wtid_incgrowthtop10 = (top10averageincomelad - l.top10averageincomelad)/l.top10averageincomelad if wtid_incgrowthtop10 == .
replace wtid_incgrowthtop10 = (top10averageincomemarriedcouples - l.top10averageincomemarriedcouples)/l.top10averageincomemarriedcouples if wtid_incgrowthtop10 == .
*replace wtid_incgrowthtop10 = 100*(top10averageincomeincludingcapit - l.top10averageincomeincludingcapit)/l.top10averageincomeincludingcapit if wtid_incgrowthtop10 == .

gen     wtid_incgrowthtop5 = (top5averageincome - l.top5averageincome)/l.top5averageincome
replace wtid_incgrowthtop5 = (top5averageincomeadults - l.top5averageincomeadults)/l.top5averageincomeadults if wtid_incgrowthtop5 == .
replace wtid_incgrowthtop5 = (top5averageincomelad - l.top5averageincomelad)/l.top5averageincomelad if wtid_incgrowthtop5 == .
replace wtid_incgrowthtop5 = (top5averageincomemarriedcoupless - l.top5averageincomemarriedcoupless)/l.top5averageincomemarriedcoupless if wtid_incgrowthtop5 == .
*replace wtid_incgrowthtop5 = 100*(top5averageincomeincludingcapita - l.top5averageincomeincludingcapita)/l.top5averageincomeincludingcapita if wtid_incgrowthtop5 == .

gen     wtid_incgrowthtop1 = (top1averageincome - l.top1averageincome)/l.top1averageincome
replace wtid_incgrowthtop1 = (top1averageincomeadults - l.top1averageincomeadults)/l.top1averageincomeadults if wtid_incgrowthtop1 == .
replace wtid_incgrowthtop1 = (top1averageincomelad - l.top1averageincomelad)/l.top1averageincomelad if wtid_incgrowthtop1 == .
replace wtid_incgrowthtop1 = (top1averageincomemarriedcoupless - l.top1averageincomemarriedcoupless)/l.top1averageincomemarriedcoupless if wtid_incgrowthtop1 == .
*replace wtid_incgrowthtop1 = 100*(top1averageincomeincludingcapita - l.top1averageincomeincludingcapita)/l.top1averageincomeincludingcapita if wtid_incgrowthtop1 == .

gen     wtid_incgrowthtop05 = (top05averageincome - l.top05averageincome)/l.top05averageincome
replace wtid_incgrowthtop05 = (top05averageincomeadults - l.top05averageincomeadults)/l.top05averageincomeadults if wtid_incgrowthtop05 == .
replace wtid_incgrowthtop05 = (top05averageincomelad - l.top05averageincomelad)/l.top05averageincomelad if wtid_incgrowthtop05 == .
replace wtid_incgrowthtop05 = (top05averageincomemarriedcouples - l.top05averageincomemarriedcouples)/l.top05averageincomemarriedcouples if wtid_incgrowthtop05 == .
*replace wtid_incgrowthtop05 = 100*(top05averageincomeincludingcapita - l.top05averageincomeincludingcapita)/l.top05averageincomeincludingcapita if wtid_incgrowthtop05 == .

gen     wtid_incgrowthtop01 = (top01averageincome - l.top01averageincome)/l.top01averageincome
replace wtid_incgrowthtop01 = (top01averageincomeadults - l.top01averageincomeadults)/l.top01averageincomeadults if wtid_incgrowthtop01 == .
replace wtid_incgrowthtop01 = (top01averageincomelad - l.top01averageincomelad)/l.top01averageincomelad if wtid_incgrowthtop01 == .
replace wtid_incgrowthtop01 = (top01averageincomemarriedcouples - l.top01averageincomemarriedcouples)/l.top01averageincomemarriedcouples if wtid_incgrowthtop01 == .
*replace wtid_incgrowthtop01 = 100*(top01averageincomeincludingcapita - l.top01averageincomeincludingcapita)/l.top01averageincomeincludingcapita if wtid_incgrowthtop01 == .

gen     wtid_incgrowthtop001 = (top001averageincome - l.top001averageincome)/l.top001averageincome
replace wtid_incgrowthtop001 = (top001averageincomeadults - l.top001averageincomeadults)/l.top001averageincomeadults if wtid_incgrowthtop001 == .
replace wtid_incgrowthtop001 = (top001averageincomelad - l.top001averageincomelad)/l.top001averageincomelad if wtid_incgrowthtop001 == .
replace wtid_incgrowthtop001 = (top001averageincomemarriedcouple - l.top001averageincomemarriedcouple)/l.top001averageincomemarriedcouple if wtid_incgrowthtop001 == .
*replace wtid_incgrowthtop001 = 100*(top001averageincomeincludingcapita - l.top001averageincomeincludingcapita)/l.top001averageincomeincludingcapita if wtid_incgrowthtop001 == .

gen     wtid_incgrowthb90 = (bottom90averageincome - l.bottom90averageincome)/l.bottom90averageincome
replace wtid_incgrowthb90 = (bottom90averageincomeadults - l.bottom90averageincomeadults)/l.bottom90averageincomeadults if wtid_incgrowthb90 == .
replace wtid_incgrowthb90 = (bottom90averageincomelad - l.bottom90averageincomelad)/l.bottom90averageincomelad if wtid_incgrowthb90 == .
replace wtid_incgrowthb90 = (bottom90averageincomemarriedcoup - l.bottom90averageincomemarriedcoup)/l.bottom90averageincomemarriedcoup if wtid_incgrowthb90 == .
*replace wtid_incgrowthb90 = 100*(bottom90averageincomeincludingca - l.bottom90averageincomeincludingca)/l.bottom90averageincomeincludingca if wtid_incgrowthb90 == . 

gen     wtid_incgrowthmean = (averageincomepertaxunit - l.averageincomepertaxunit)/l.averageincomepertaxunit
replace wtid_incgrowthmean = (averageincomeperadult - l.averageincomeperadult)/l.averageincomeperadult if wtid_incgrowthmean == . 
replace wtid_incgrowthmean = (averageincomepertaxunitadults - l.averageincomepertaxunitadults)/l.averageincomepertaxunitadults if wtid_incgrowthmean == .
replace wtid_incgrowthmean = (averageincomepertaxunitmarriedco - l.averageincomepertaxunitmarriedco)/l.averageincomepertaxunitmarriedco if wtid_incgrowthmean == .
*replace wtid_incgrowthmean = (averageincomepertaxunitbreakin19 - l.averageincomepertaxunitbreakin19)/l.averageincomepertaxunitbreakin19 if wtid_incgrowthmean == .
*replace wtid_incgrowthmean = (averageincomepertaxunitincluding - l.averageincomepertaxunitincluding)/l.averageincomepertaxunitincluding if wtid_incgrowthmean == . 


gen     share10 = top10incomeshare
replace share10 = top10incomeshareadults if share10 == .
replace share10 = top10incomesharelad if share10 == .
replace share10 = top10incomesharetaxdata if share10 == .
replace share10 = top10incomeshareids if share10 == .
replace share10 = top10incomesharemarriedcouplessi if share10 == .

gen     share5 = top5incomeshare
replace share5 = top5incomeshareadults if share5 == .
replace share5 = top5incomesharelad if share5 == .
replace share5 = top5incomesharetaxdata if share5 == .
replace share5 = top5incomeshareids if share5 == .
replace share5 = top5incomesharemarriedcouplessin if share5 == .

gen     share1 = top1incomeshare
replace share1 = top1incomeshareadults if share1 == .
replace share1 = top1incomesharelad if share1 == .
replace share1 = top1incomesharetaxdata if share1 == .
replace share1 = top1incomeshareids if share1 == .
replace share1 = top1incomesharemarriedcouplessin if share1 == .

gen     share01 = top01incomeshare
replace share01 = top01incomeshareadults if share01 == .
replace share01 = top01incomesharelad if share01 == .
* These variables don't exist
*replace share01 = top01incomesharetaxdata if share01 == .
*replace share01 = top01incomeshareids if share01 == .
replace share01 = top01incomesharemarriedcouplessi if share01 == .


keep country year wtid_incgrowthtop1 wtid_incgrowthtop5 wtid_incgrowthtop10 wtid_incgrowthb90 wtid_incgrowthmean wtid_incgrowthtop01 wtid_incgrowthtop05 wtid_incgrowthtop001 share10 share5 share1 share01 top5averageincome*

replace country = "UK" if country == "United Kingdom"
replace country = "USA" if country == "United States"

sort country year

save "$gendatadir/wtid_processed.dta", replace
